package com.wc.AlgoOJ_train.train1.c4.A409;

import java.util.Scanner;

/**
 * @Author congge
 * @Date 2024/1/9 15:16
 * @description 又见GCD
 * 12 6 -> 18
 * http://43.138.190.70:8888/p/A409
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while (n-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = 2 * b;
            while (gcd(a, c) > b) {
                c += b;
            }
            System.out.println(c);
        }
    }

    public static int gcd(int a, int b) {
        if (a > b) {
            return gcd(b, a);
        }
        while (b % a != 0) {
            int tmp = b % a;
            b = a;
            a = tmp;
        }
        return a;
    }
}
